package com.atguigu.gmall.ums.service.impl;

import com.atguigu.gmall.common.exception.AuthException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.ums.mapper.UserMapper;
import com.atguigu.gmall.ums.entity.UserEntity;
import com.atguigu.gmall.ums.service.UserService;

import java.util.Date;
import java.util.UUID;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<UserEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<UserEntity>()
        );

        return new PageResultVo(page);
    }

    /**
     * 校验数据
     * @param data
     * @param type
     * @return
     */
    @Override
    public Boolean checkData(String data, Integer type) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        //校验数据 判断类型
        switch (type){
            case 1 : queryWrapper.eq(UserEntity::getUsername,data);break;
            case 2 : queryWrapper.eq(UserEntity::getPhone,data);break;
            case 3 : queryWrapper.eq(UserEntity::getEmail,data);break;
            default:
                return null;
        }
        return this.count(queryWrapper) == 0;
    }

    /**
     * 用户登录 保存用户信息
     * @param userEntity
     * @param code
     */
    @Override
    public void register(UserEntity userEntity, String code){

        // TODO:获取手机号 通过短信微服务发送验证码 比较验证码是否相同 code

        //生成盐
        String salt = StringUtils.substring(UUID.randomUUID().toString(),0,6);
        userEntity.setSalt(salt);

        //对密码进行加密
        userEntity.setPassword(DigestUtils.md5Hex(userEntity.getPassword()+salt));

        //设置其他参数信息
        userEntity.setNickname(userEntity.getUsername());
        userEntity.setLevelId(1L);
        userEntity.setSourceType(1);
        userEntity.setIntegration(1000);
        userEntity.setGrowth(1000);
        userEntity.setStatus(1);
        userEntity.setCreateTime(new Date());

        //保存用户信息
        this.save(userEntity);

        //删除验证码信息
    }

    /**
     * 查询用户信息
     * @param loginName
     * @param password
     * @return
     */
    @Override
    public UserEntity queryOne(String loginName, String password) {

        //根据用户名查询用户信息 用户名 手机号 邮箱
        UserEntity userEntity = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUsername, loginName).or()
                .eq(UserEntity::getPhone, loginName).or()
                .eq(UserEntity::getEmail, loginName));
        //判断用户是否存在 自定义异常信息
        if (userEntity == null){
            //用户不存在 抛出异常
            throw new AuthException("信息错误！");
        }

        //获取输入的密码 进行加盐加密 和数据库密码进行比较
        password = DigestUtils.md5Hex(password+userEntity.getSalt());
        if (!StringUtils.equals(password,userEntity.getPassword())){
            //密码不相等 抛出异常
            throw new AuthException("信息错误！");
        }
        //信息无误 返回用户信息
        return userEntity;
    }
}